<?php

declare (strict_types=1);

namespace app\controller\admin;

use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\model\UserGroup;
use app\model\Menu;

class Group extends Common
{
    public function index($act = null, $id = null)
    {
        if ($act == 'add') {
            $auth = Menu::field('id,pid,title')->order('o asc')->select()->toArray();
            $auth = categorize_and_sort_hierarchy($auth);
            View::assign('auth', $auth);
            View::assign('group', ['auth' => []]);
            return View::fetch('form');
        }

        if ($act == 'edit') {
            $id = intval($id);
            $group = Db::name('user_group')->where(['id' => $id])->find($id);
            if (!$group) {
                return error(lang('参数错误'));
            }
            $group['auth'] = explode(',', $group['auth']);
            $auth = Menu::field('id,pid,title')
                    ->order('o asc')
                    ->select()
                    ->toArray();
            $auth = categorize_and_sort_hierarchy($auth);

            View::assign('group', $group);
            View::assign('auth', $auth);
            return View::fetch('form');
        }

        if ($act == 'update') {
            if (!Request::isPost()) {
                return error(lang('参数错误'));
            }
            $data = input('post.');
            if ($data['title'] == '') {
                return error(lang('用户组名不能为空'));
            }
            if (isset($data['status']) and $data['status'] == 1) {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            if (isset($data['auth'])) {
                $data['auth'] = implode(',', $data['auth']);
            } else {
                $data['auth'] = '';
            }

            if ($data['id'] > 0) {
                $userGroup = UserGroup::find($data['id']);
                $r = $userGroup->save($data);
                if ($r) {
                    add_log('修改用户组，ID：'.$data['id'], $this->request->user->username);
                    return success(lang('操作成功'), (string) url('admin/group/index'));
                }
            } else {
                unset($data['id']);
                $userGroup = new UserGroup();
                $r = $userGroup->save($data);
                if ($r) {
                    add_log('新增用户组，ID：'.$userGroup->id, $this->request->user->username);
                    return success(lang('操作成功'), (string) url('admin/group/index'));
                } else {
                    return error(lang('系统错误'));
                }
            }
        }

        if ($act == 'del') {
            if (!Request::isPost()) {
                return $this->error(lang('参数错误'));
            }
            $ids = input('post.');

            if (!empty($ids)) {
                $r = UserGroup::destroy($ids['ids']);
                if ($r) {
                    add_log('删除用户组，ID：'.implode(',', $ids['ids']), $this->request->user->username);
                    return success(lang('操作成功'), (string) url('admin/group/index'));
                }
            }

            return $this->error(lang('请勾选需要删除的选项'));
        }

        $list = UserGroup::field('id,title,status')->select();
        View::assign('list', $list);
        return View::fetch();
    }
}
